From 53c9374c9f6a72e3c9db0d3c524233f2a04d58a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jes=C3=BAs=20Espino?= Date: Fri, 4 Mar 2016 18:34:30 +0100 Subject: [PATCH] Add limit parameter to search command (fix issue #2402) --- src/bin/search.rs | 7 ++++++- src/cargo/ops/registry.rs | 7 +++++-- src/crates-io/lib.rs | 4 ++-- tests/test_cargo_search.rs | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/bin/search.rs b/src/bin/search.rs index fa8d792c2..8b8c010e6 100644 --- a/src/bin/search.rs +++ b/src/bin/search.rs @@ -1,12 +1,15 @@ use cargo::ops; use cargo::util::{CliResult, Config}; +use std::cmp; + #[derive(RustcDecodable)] pub struct Options { flag_host: Option, flag_verbose: Option, flag_quiet: Option, flag_color: Option, + flag_limit: Option, arg_query: String } @@ -23,6 +26,7 @@ Options: -v, --verbose Use verbose output -q, --quiet No output printed to stdout --color WHEN Coloring: auto, always, never + --limit LIMIT Limit the number of results (default: 10, max: 100) "; pub fn execute(options: Options, config: &Config) -> CliResult> { @@ -31,10 +35,11 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { &options.flag_color)); let Options { flag_host: host, + flag_limit: limit, arg_query: query, .. } = options; - try!(ops::search(&query, config, host)); + try!(ops::search(&query, config, host, cmp::min(100, limit.unwrap_or(10)) as u8)); Ok(None) } diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 0b2f83e18..da2d80022 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -340,7 +340,10 @@ pub fn yank(config: &Config, Ok(()) } -pub fn search(query: &str, config: &Config, index: Option) -> CargoResult<()> { +pub fn search(query: &str, + config: &Config, + index: Option, + limit: u8) -> CargoResult<()> { fn truncate_with_ellipsis(s: &str, max_length: usize) -> String { if s.len() < max_length { s.to_string() @@ -350,7 +353,7 @@ pub fn search(query: &str, config: &Config, index: Option) -> CargoResul } let (mut registry, _) = try!(registry(config, None, index)); - let crates = try!(registry.search(query).map_err(|e| { + let crates = try!(registry.search(query, limit).map_err(|e| { human(format!("failed to retrieve search results from the registry: {}", e)) })); diff --git a/src/crates-io/lib.rs b/src/crates-io/lib.rs index ea19883be..0586c76f2 100644 --- a/src/crates-io/lib.rs +++ b/src/crates-io/lib.rs @@ -170,8 +170,8 @@ impl Registry { Ok(()) } - pub fn search(&mut self, query: &str) -> Result> { - let body = try!(self.req(format!("/crates?q={}", query), None, Get, + pub fn search(&mut self, query: &str, limit: u8) -> Result> { + let body = try!(self.req(format!("/crates?q={}&per_page={}", query, limit), None, Get, Auth::Unauthorized)); Ok(json::decode::(&body).unwrap().crates) diff --git a/tests/test_cargo_search.rs b/tests/test_cargo_search.rs index db754ffe2..d4c483872 100644 --- a/tests/test_cargo_search.rs +++ b/tests/test_cargo_search.rs @@ -76,7 +76,7 @@ test!(simple { // from source there anyway! File::create(&base).unwrap().write_all(contents.as_bytes()).unwrap(); if !cfg!(windows) { - File::create(&base.with_file_name("crates?q=postgres")).unwrap() + File::create(&base.with_file_name("crates?q=postgres&per_page=10")).unwrap() .write_all(contents.as_bytes()).unwrap(); } -- 2.30.2